package com.WFinstitute.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.WFinstitute.domain.Test;
import com.WFinstitute.service.TestService;
import com.WFinstitute.util.Page;
import com.WFinstitute.util.TimeUtil;



/**
 * 
 * @author 匹夫
 * @date 2016-7-15 下午1:29:33
 * @function 测试jpa类
 */

@Controller
@RequestMapping("/test")
public class TestController {
	@Resource
	private TestService testService;
	
	@RequestMapping("/test")
	public String test(HttpServletResponse response,HttpServletRequest request){
		return "/WEB-INF/views/test.jsp";
	}
	
	/**
	 * 添加一条数据
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping("/add")
	public String add(HttpServletResponse response,HttpServletRequest request){
		Test test=new Test();
		//获取当前时间
		String currentTime = TimeUtil.getCurrentTime();
		test.setName("wangzekun");
		test.setDescription(currentTime);
		testService.addTest(test);
		return null;
	}
	
	/**
	 * 列表 不分页
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(HttpServletResponse response,HttpServletRequest request){
		List<Test> testList = testService.findTestList();
		for (Test test : testList) {
			System.out.println(test.getName());
		}
		return null;
	}
	
	@RequestMapping("/pagelist")
	public String pageList(HttpServletResponse response,HttpServletRequest request){
		int pageIndex=request.getParameter("pageIndex")!=null?Integer.valueOf(request.getParameter("pageIndex")):1;
		int pageSize=request.getParameter("pageSize")!=null?Integer.valueOf(request.getParameter("pageSize")):10;
		Page<Test> pageList = testService.findTestList(pageSize, pageIndex, "wangzekun");
		List<Test> testList = pageList.getList();
		for (Test test : testList) {
			System.out.println(test.getDescription());
		}
		return null;
	}
	
	@RequestMapping("/update")
	public String update(HttpServletResponse response,HttpServletRequest request){
		Test test = testService.findTestById(1);
		test.setName("wangzekun_xiugai");
		testService.updateTest(test);
		return null;
	}
	
}
